package lesson12;

import jdk.swing.interop.SwingInterOpUtils;

import java.io.*;
import java.util.Properties;

public class Test04 {
    public void inputStream(){
        try {
            FileInputStream fis = new FileInputStream("1.txt");

//            int i;
//            while((i =  fis.read()) != -1){
//                System.out.print((char)i);
//            }
            byte [] b = new byte[1024];
            int len = 0;
            while((len = fis.read(b)) != -1){
                System.out.println(new String(b,0,len));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void reader(){
        try {
            FileReader reader = new FileReader("1.txt");
            BufferedReader br = new BufferedReader(reader);
//            int i = 0;
//            while((i = reader.read()) != -1){
//                System.out.print((char)i);
//            }
//            int len = 0;
//            char [] c = new char[1024];
//            while((len = reader.read(c)) != -1){
//                System.out.print(new String(c,0,len));
//            }
            String str = null;
            while((str = br.readLine()) != null){
                System.out.println(str);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void readProperties(){
        Properties prop = new Properties();
        try {
            prop.load(new FileInputStream("1.properties"));
            System.out.println(prop.getProperty("name"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void writeProperties(){
        try {
            Properties prop = new Properties();
            prop.load(new FileInputStream("1.properties"));
            prop.setProperty("gender","男");
            prop.store(new FileOutputStream("1.properties"),null);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void deleteDirs(String path){
        File file = new File(path);
        File [] files = file.listFiles();
        for(File f : files){
            if(f.isFile()){
                f.delete();
//                System.out.println(f.getAbsolutePath());
            }else{
                deleteDirs(f.getAbsolutePath());
            }
        }
        file.delete();
//        System.out.println(file.getAbsolutePath());
    }
    public static void main(String[] args) {
        Test04 test04 = new Test04();
//        test04.reader();
//        test04.readProperties();
//        test04.writeProperties();
        test04.deleteDirs("D:\\java21.7homework");
    }
}
